<?php
header("Content-Type:text/html;charset=utf-8");

$content = file_get_contents('http://desk.zol.com.cn/qiche/');

$allpic = '/<span\s*class="allPic">.*<font>(.*)<\/font>.*<\/span>/imsU';

preg_match($allpic,$content,$res);

// var_dump($res);

$count = isset($res[1]) ? $res[1] : 0;

// echo $count; // 821

if($count < 0)
{
    echo '无图片采集';
    exit;
}

$limit = 21;

$pages = ceil($count / $limit);

// 报错多少个图片
$number = 0;

// 循环 => 图片地址放在这个数组
$UrlList = [];

$url = 'http://desk.zol.com.cn/qiche/';

for($i = 1;$i <= $pages;$i++)
{
    $UrlList[] = $url . $i . '.html';
}



foreach($UrlList as $k => $item)
{
    // 延迟执行
    sleep(1);

    // 请求每页的列表
    $Content = file_get_contents($item);

    // var_dump($Content);

    $ulPreg = '/<ul\s*class="pic-list2\s*clearfix">(.*)<\/ul>/imsU';

    preg_match($ulPreg,$Content,$res);

    $resImge = isset($res[1]) ? $res[1] : [];


    if(empty($resImge))
    {
        continue;
    }

    // <img width="208px" height="130px"  alt="经典跑车高清图片壁纸" src="https://desk-fd.zol-img.com.cn/t_s208x130c5/g6/M00/0C/0A/ChMkKV9ti0OINUb4ADCGtlNV8wQAACyQgEjuMoAMIbO670.jpg" title = "经典跑车高清图片壁纸"/>
    $reImg = '/<img.*src="(.*)".*>/imsU';

    preg_match_all($reImg,$resImge,$res);

    // var_dump($res);
    $imgList  = isset($res[1]) ? $res[1] : [];

    // var_dump($imgList);

    if (empty($imgList)) {
        continue;
    }

    foreach($imgList as $key => $value)
    {
        $imgContent = file_get_contents($value);


        $filename = pathinfo($value,PATHINFO_BASENAME);

        $result = file_put_contents('./image/'.$filename,$imgContent);

        $result > 0 && $number++;
    }

    

}
echo "保存了{$number}张图片";